package com.dongdong.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dongdong.em.Result;
import com.dongdong.model.TbUser;
import com.dongdong.service.AddressService;
import com.dongdong.util.JWTUtil;
import com.dongdong.vo.AddressVO;
import com.dongdong.vo.SellerVO;
import com.dongdong.vo.UserVO;
import io.jsonwebtoken.Claims;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Type;
import java.util.List;

@RestController
@RequestMapping("address")
@RefreshScope    //热更新
public class AddressController {

    @Autowired
    private AddressService addressService;

    @Autowired
    private JWTUtil jwtUtil;


    @GetMapping("getAddress")
    public Result getAddress(@RequestHeader String Autherizetion){
        //从token中获取当前用户信息
        Claims claims = jwtUtil.parseJwt(Autherizetion);
        String subject = claims.getSubject();
        //将拿到的用户信息放进实体类接收
        TbUser userVO = JSON.parseObject(subject, TbUser.class);//用vo接收一下解析好的用户信息
        String username = userVO.getUsername();

        //根据用户名称去查询
        List<AddressVO> addressVOList = addressService.getAddressByUserName(username);
        return Result.OK().setData(addressVOList);
    }

    @GetMapping("getAddressById")
    public Result getAddressById(@RequestParam Long addressId){
        AddressVO addressVO = addressService.getAddressById(addressId);
        return Result.OK().setData(addressVO);
    }
}
